import { TableColumnProps } from "ant-design-vue";
import {
  AdjustmentType,
  DeliveryType,
  GoodsSaleType,
  ProductFileItem,
  ProfitLossType,
  PurchaseOrderType,
  ReceiptItem,
  ReceiptType,
  SupplierItem,
  WarehouseItem,
} from "/@/api/main/pis/models";
import { h, ref } from "vue";
import { FormItemComponentType, FormItemData } from "/@/components/CURDDialog/models";
import { getSupplierList, getWarehouseList } from "/@/api/main/pis";
import { enumToOptions } from "/@/utils/common";
import SelectInput from "/@/components/common/SelectInput.vue";

export function useReceiptForm(receiptType: ReceiptType) {
  let innerModel: Partial<ReceiptItem> = {};
  if (receiptType === ReceiptType.delivery) {
    innerModel = {
      // deliveryNo: "",
      deliveryType: null,
      warehouseId: null,
      warehouseAdmin: "",
      deliveryDate: null,
      remark: "",
    };
  } else if (receiptType === ReceiptType.warehousing) {
    innerModel = {
      // warehousingNo: "",
      supplierId: null,
      warehouseId: null,
      warehouseAdmin: "",
      warehousingDate: null,
      remark: "",
    };
  } else if (receiptType === ReceiptType.return) {
    innerModel = {
      // supplierId: null,
      // returnsNo: "",
      // returnsWarehouse: null,
      returnsWarehouseId: null,
      warehouseAdmin: "",
      returnsDate: null,
      remark: "",
    };
  } else if (receiptType === ReceiptType.adjustment) {
    innerModel = {
      // adjustmentNo: "",
      adjustmentType: null,
      deliveryWarehouseId: null,
      deliveryAdmin: "",
      receiveWarehouseId: null,
      receiveAdmin: "",
      adjustmentDate: null,
      remark: "",
    };
  } else if (receiptType === ReceiptType.profitLoss) {
    innerModel = {
      profitLossNo: "",
      profitLossType: null,
      warehouseId: null,
      warehouseAdmin: "",
      profitLossDate: null,
      remark: "",
    };
  } else if (receiptType === ReceiptType.purchase) {
    innerModel = {
      // orderNo: "",
      supplierId: null,
      orderType: null,
      purchasingOfficer: "",
      warehouseId: null,
      deliveryDate: null,
      remark: "",
    };
  } else if (receiptType === ReceiptType.sell) {
    innerModel = {
      // goodsSaleNo: "",
      warehouseId: null,
      warehouseAdmin: "",
      goodsSaleType: null,
      saleDate: null,
      remark: "",
    };
  }

  const model = ref<Partial<ReceiptItem>>(innerModel);

  const warehouseData = ref<WarehouseItem[] | null>();
  const supplierData = ref<SupplierItem[] | null>();
  const operatorText = ref("");
  const operatorKey = ref("");
  const operatorSelVisible = ref(false);

  function getWareHouseItem(key: string): FormItemData {
    return {
      key,
      type: FormItemComponentType.SELECT,
      props: {
        options: [],
      },
      async getData() {
        warehouseData.value = await getWarehouseList();
        return warehouseData.value
          ? warehouseData.value.map((item) => ({
              label: item.name,
              value: item.name,
            }))
          : [];
      },
      required: true,
    };
  }
  function getWareHouseIdItem(key: string, label?: string): FormItemData {
    return {
      key,
      label: label || "仓库",
      type: FormItemComponentType.SELECT,
      props: {
        options: [],
      },
      async getData() {
        warehouseData.value = await getWarehouseList();
        return warehouseData.value
          ? warehouseData.value.map((item) => ({
              label: item.name,
              value: item.id,
            }))
          : [];
      },
      required: true,
    };
  }

  const modelFormSetting = ref<FormItemData[]>([
    { key: "warehousingNo", required: true },
    { key: "deliveryNo", required: true },
    { key: "returnsNo", required: true },
    { key: "adjustmentNo", required: true },
    { key: "profitLossNo", required: true },
    { key: "orderNo", required: true },
    { key: "goodsSaleNo", required: true },
    {
      key: "deliveryType",
      type: FormItemComponentType.SELECT,
      required: true,
      props: {
        options: enumToOptions(DeliveryType, "deliveryType"),
      },
    },
    {
      key: "adjustmentType",
      type: FormItemComponentType.SELECT,
      required: true,
      props: {
        options: enumToOptions(AdjustmentType, "adjustmentType"),
      },
    },
    {
      key: "profitLossType",
      type: FormItemComponentType.SELECT,
      required: true,
      props: {
        options: enumToOptions(ProfitLossType, "profitLossType"),
      },
    },
    {
      key: "orderType",
      type: FormItemComponentType.SELECT,
      required: true,
      props: {
        options: enumToOptions(PurchaseOrderType, "purchaseOrderType"),
      },
    },
    {
      key: "goodsSaleType",
      type: FormItemComponentType.SELECT,
      required: true,
      props: {
        options: enumToOptions(GoodsSaleType, "goodsSaleType"),
      },
    },
    {
      key: "supplierName",
      type: FormItemComponentType.SELECT,
      props: {
        options: [],
      },
      async getData() {
        supplierData.value = await getSupplierList();
        return supplierData.value
          ? supplierData.value.map((item) => ({
              label: item.name,
              value: item.name,
              key: item.id,
            }))
          : [];
      },
      required: true,
    },
    {
      key: "supplierId",
      label: "供应商",
      type: FormItemComponentType.SELECT,
      props: {
        options: [],
      },
      async getData() {
        supplierData.value = await getSupplierList();
        return supplierData.value
          ? supplierData.value.map((item) => ({
              label: item.name,
              value: item.id,
              key: item.id,
            }))
          : [];
      },
      required: true,
    },
    getWareHouseItem("returnsWarehouse"),
    getWareHouseItem("deliveryWarehouse"),
    getWareHouseItem("receiveWarehouse"),
    getWareHouseIdItem("warehouseId"),
    getWareHouseIdItem("returnsWarehouseId", "退回仓库"),
    getWareHouseIdItem("deliveryWarehouseId", "出货仓库"),
    getWareHouseIdItem("receiveWarehouseId", "收货仓库"),
    {
      key: "purchasingOfficer",
      customComponent: h(SelectInput, {
        onClick() {
          operatorText.value = "采购员";
          operatorKey.value = "purchasingOfficer";
          operatorSelVisible.value = true;
        },
      }),
    },
    {
      key: "warehouseAdmin",
      customComponent: h(SelectInput, {
        onClick() {
          operatorText.value = "仓库管理员";
          operatorKey.value = "warehouseAdmin";
          operatorSelVisible.value = true;
        },
      }),
    },
    {
      key: "deliveryAdmin",
      customComponent: h(SelectInput, {
        onClick() {
          operatorText.value = "出货管理员";
          operatorKey.value = "deliveryAdmin";
          operatorSelVisible.value = true;
        },
      }),
    },
    {
      key: "receiveAdmin",
      customComponent: h(SelectInput, {
        onClick() {
          operatorText.value = "收货管理员";
          operatorKey.value = "receiveAdmin";
          operatorSelVisible.value = true;
        },
      }),
    },
    { key: "profitLossDate", type: FormItemComponentType.DATEPICKER, props: { showTime: false } },
    { key: "warehousingDate", type: FormItemComponentType.DATEPICKER, props: { showTime: false } },
    { key: "adjustmentDate", type: FormItemComponentType.DATEPICKER, props: { showTime: false } },
    { key: "deliveryDate", type: FormItemComponentType.DATEPICKER, props: { showTime: false } },
    { key: "returnsDate", type: FormItemComponentType.DATEPICKER, props: { showTime: false } },
    { key: "saleDate", type: FormItemComponentType.DATEPICKER, props: { showTime: true } },
    // { key: "tabulationTime", type: FormItemComponentType.DATEPICKER, props: { showTime: true } },
    // { key: "tabulator", label: "制表人" },
    { key: "remark", label: "备注" },
  ]);
  for (let i = modelFormSetting.value.length - 1; i >= 0; i--) {
    if (model.value[modelFormSetting.value[i].key] === undefined) {
      modelFormSetting.value.splice(i, 1);
    }
  }
  return {
    model,
    modelFormSetting,
    supplierData,
    warehouseData,
    operatorKey,
    operatorText,
    operatorSelVisible,
  };
}
